package com.kj.xuyuan.login;

import androidx.appcompat.app.AppCompatActivity;
import io.rong.imkit.RongIM;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;

import com.kj.xuyuan.MainActivity;
import com.kj.xuyuan.R;
import com.kj.xuyuan.base.BaseActivity;
import com.kj.xuyuan.base.Constants;
import com.kj.xuyuan.pop.FirstOpenPopWindow;
import com.kj.xuyuan.util.SPUtils;

public class SplashActivity extends BaseActivity {

    private static final int GO_HOME = 1;
    private static final int GO_LOGIN = 3;
    // 延迟2秒
    private static final long SPLASH_DELAY_MILLIS = 2000;

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initView() {

    }

    private void goHome() {
        handler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
    }

    private void goLogin() {
        handler.sendEmptyMessageDelayed(GO_LOGIN, SPLASH_DELAY_MILLIS);
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GO_HOME:
                    startActivity(new Intent(context, MainActivity.class));
                    finish();
                    break;
                case GO_LOGIN:
                    startActivity(new Intent(context,OneKeyLoginActivity.class));
                    finish();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus){
            next();
        }
    }

    private void next() {
        if(!SPUtils.getBooleanData(SplashActivity.this, Constants.IS_FIRST_OPEN)){// 首次打开app
            popFirstOpenWindow();
        }else{
            if (SPUtils.getBooleanData(SplashActivity.this, Constants.IS_LOGIN)) {
                goHome();
            } else {
                goLogin();
            }
        }
    }

    private void popFirstOpenWindow() {
        FirstOpenPopWindow popWindow = new FirstOpenPopWindow(SplashActivity.this);
        popWindow.showAtLocation(getWindow().getDecorView(), Gravity.CENTER,0,0);
        popWindow.setAnimationStyle(R.style.popwindow_center_anim_style);

        popWindow.setOnClickListener(new FirstOpenPopWindow.OnAgreeListener() {
            @Override
            public void clickAgree() {
//                RongIM.init(getApplication(), "x18ywvqfx4ooc");
                SPUtils.setBooleanData(SplashActivity.this, "isFirstOpen",true);
                goLogin();
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }
}